package com.example.a313ldy.yuejian_17.Model;

import com.example.a313ldy.yuejian_17.Bean.Master;
import com.example.a313ldy.yuejian_17.Gloablecalss.Yuejian;
import com.example.a313ldy.yuejian_17.Interface.MasterInterface;
import com.example.a313ldy.yuejian_17.Listener.MasterListener;
import com.example.a313ldy.yuejian_17.Service.MasterService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by LDY on 2017/10/31.
 */

public class MasterModel implements MasterInterface {
    private MasterService service;
    private Retrofit retrofit;

    public MasterModel() {
        retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Yuejian.URL)
                .build();

    }




    @Override
    public void MasterData(final MasterListener<Master> listener) {
        service= retrofit.create(MasterService.class);
        Call<Master> call = service.getMaster(1);
        //Retrofit使用-3
        call.enqueue(new Callback<Master>() {
            @Override
            public void onResponse(Call<Master> call, Response<Master> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<Master> call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }
}
